package com.rongshu.view.userInfo.activity;

import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.rongshu.R;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.discovery.activity.UserHomePageActivity;
import com.rongshu.view.discovery.bean.ArticleDetaisRequest;
import com.rongshu.view.home.activity.SingleArticleDetailsActivity;
import com.rongshu.view.userInfo.adapter.CommentMessageAdapter;
import com.rongshu.view.userInfo.entity.CommentMeMessage;
import com.rongshu.view.userInfo.iview.ICommentMeListView;
import com.rongshu.view.userInfo.presenter.CommentMeListPresenter;

import java.util.ArrayList;
import java.util.List;

/**
 * created by zhengweis on 2019/7/9.
 * description：
 */
public class CommentMeListActivity extends MessageListActivity<CommentMeListPresenter> implements ICommentMeListView {
    //adapter
    private CommentMessageAdapter adapter = null;
    //点赞列表
    private List<CommentMeMessage.ResponseBean> commentMsgList;
    //标记点击item
    private int position;

    @Override
    protected void initView() {
        super.initView();
        headMiddle.setText("留言");
        setPresenter(new CommentMeListPresenter(this), this);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(manager);
        commentMsgList = new ArrayList<>();
        adapter = new CommentMessageAdapter(this, commentMsgList);
        adapter.setHasStableIds(true);
        //动画
        DefaultItemAnimator defaultItemAnimator = new DefaultItemAnimator();
        defaultItemAnimator.setRemoveDuration(100);
        recyclerView.setItemAnimator(defaultItemAnimator);
        adapter.setOnItemClickListener(this);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void requestMessage() {
        presenter.requestCommentMessage(pageIndex, pageSize);
    }

    @Override
    protected void setAllRead() {
        presenter.GetMsgAllRead();
        for (int index = 0; index < commentMsgList.size(); index++) {
            commentMsgList.get(index).setIsRead(true);
        }
        adapter.notifyDataSetChanged();
    }

    @Override
    public void onItemClick(int position) {
        this.position = position;
        if (!commentMsgList.get(position).isIsRead()) {
            presenter.GetSetReadCommentMsg(commentMsgList.get(position).getCommentId());
        }
        Intent intent = new Intent(this, SingleArticleDetailsActivity.class);
        ArticleDetaisRequest bean = new ArticleDetaisRequest();
        bean.setContribute_id(commentMsgList.get(position).getObjId());
        bean.setType(Constant.FROM_HISTORY);
        bean.setTopic_id(0);
        intent.putExtra(Constant.ARTICLE_DETAILS_REQUEST, bean);
        startActivity(intent);
    }

    @Override
    public void onItemDeleteClick(int position) {
        CommentMeMessage.ResponseBean bean = commentMsgList.get(position);
        presenter.getCommentOrReplyDelete(bean.getObjId(), bean.getCommentId(), 0);
        commentMsgList.remove(position);
        adapter.notifyItemRemoved(position);//注意这里
        adapter.notifyItemRangeChanged(position, commentMsgList.size() - position);//通知数据与界面重新绑定
    }

    @Override
    public void onUserHeaderClick(int userIdTo) {
        Intent intent = new Intent();
        if (userIdTo == UserInfoUtils.getUserId()) {
            intent.setClass(this, PersonDetailsActivity.class);
            startActivity(intent);
        } else {
            intent.setClass(this, UserHomePageActivity.class);
            intent.putExtra(Constant.USER_ID, userIdTo);
            startActivity(intent);
        }
    }

    @Override
    public void onCommentMessageList(CommentMeMessage commentMeMessage) {
        List<CommentMeMessage.ResponseBean> authorDetailBeans = commentMeMessage.getResponse();
        if (refreshLayout.isRefreshing()) {//结束刷新
            refreshLayout.finishRefresh();
        }
        if (refreshLayout.isLoading()) {//结束加载
            refreshLayout.finishLoadmore();
        }
        if (pageIndex == 1) {
            commentMsgList.clear();
        }
        commentMsgList.addAll(authorDetailBeans);
        adapter.notifyDataSetChanged();
        if (authorDetailBeans.size() == 0) {//请求不到数据，证明没有更多了
            refreshLayout.finishLoadmoreWithNoMoreData();
        }
        if (commentMsgList.size() == 0) {//判断显示缺省页面
            showEmptyLayout();
        } else {
            hideEmptyLayout();
        }
    }

    @Override
    public void onSetReadeComment(INetBean bean) {
        if (bean.getErrCode() == 0) {
            messageNum--;
            if (messageNum <= 0) {
                txt_right.setVisibility(View.GONE);
            }
            commentMsgList.get(position).setIsRead(true);
            adapter.notifyItemChanged(position);
        } else {
            toast(bean.getErrMsg());
        }
    }
}
